package com.vict.controller.web.pay;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.vict.bean.image.ao.InsertImageAO;
import com.vict.bean.image.vo.ImageVO;
import com.vict.bean.pay.ao.CaseSouYinAO;
import com.vict.bean.pay.ao.PayBackAO;
import com.vict.bean.pay.ao.SelectPayBackOrderAO;
import com.vict.bean.pay.ao.SelectPayOrderAO;
import com.vict.bean.pay.vo.PayBackVO;
import com.vict.bean.pay.vo.PayOrderVO;
import com.vict.entity.Image;
import com.vict.framework.bean.MyPageInfo;
import com.vict.framework.bean.R;
import com.vict.framework.pay.bean.PayAwaitDTO;
import com.vict.framework.pay.entity.PayBack;
import com.vict.framework.pay.entity.PayOrder;
import com.vict.framework.pay.mapper.PayBackMapper;
import com.vict.framework.pay.mapper.PayOrderMapper;
import com.vict.framework.pay.utils.PayUtils;
import com.vict.framework.web.ApiPrePath;
import com.vict.service.impl.PayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@ApiPrePath
@Api
@Slf4j
@RestController
@RequestMapping("/pay")
public class PayController {
    private static final String SEAGGER_TAGS = "收银台";

    @Autowired
    private PayOrderMapper payOrderMapper;

    @Autowired
    private PayBackMapper payBackMapper;

    @Autowired
    private PayService payService;

    @ApiOperation(tags = SEAGGER_TAGS, value = "支付记录", httpMethod = "POST")
    @PostMapping("/selectPayOrder")
    public R<MyPageInfo<PayOrderVO>> selectPayOrder(@RequestBody SelectPayOrderAO selectPayOrderAO) {
        String payDtoId = Optional.ofNullable(selectPayOrderAO).map(o -> o.getPayDtoId()).map(o -> o.trim()).filter(o -> !o.equals("")).orElse(null);
        BigDecimal moneyBegin = Optional.ofNullable(selectPayOrderAO).map(o -> o.getMoneyBegin()).orElse(null);
        BigDecimal moneyEnd = Optional.ofNullable(selectPayOrderAO).map(o -> o.getMoneyEnd()).orElse(null);
        PayOrder.PayStatus payStatus = Optional.ofNullable(selectPayOrderAO).map(o -> o.getPayStatus()).orElse(null);
        PayOrder.PayType payType = Optional.ofNullable(selectPayOrderAO).map(o -> o.getPayType()).orElse(null);
        Timestamp startTimeBegin = Optional.ofNullable(selectPayOrderAO).map(o -> o.getStartTimeBegin()).orElse(null);
        Timestamp startTimeEnd = Optional.ofNullable(selectPayOrderAO).map(o -> o.getStartTimeEnd()).orElse(null);
        Timestamp successTimeBegin = Optional.ofNullable(selectPayOrderAO).map(o -> o.getSuccessTimeBegin()).orElse(null);
        Timestamp successTimeEnd = Optional.ofNullable(selectPayOrderAO).map(o -> o.getSuccessTimeEnd()).orElse(null);
        Timestamp cancelTimeBegin = Optional.ofNullable(selectPayOrderAO).map(o -> o.getCancelTimeBegin()).orElse(null);
        Timestamp cancelTimeEnd = Optional.ofNullable(selectPayOrderAO).map(o -> o.getCancelTimeEnd()).orElse(null);
        Integer isBack = Optional.ofNullable(selectPayOrderAO).map(o -> o.getIsBack()).orElse(null);

        // 数据查询
        Page page = PageHelper.startPage(selectPayOrderAO.getPageNum(), selectPayOrderAO.getPageSize());

        payOrderMapper.selectMyList(
            payDtoId, moneyBegin, moneyEnd, payStatus, payType, startTimeBegin, startTimeEnd, successTimeBegin, successTimeEnd, cancelTimeBegin, cancelTimeEnd, isBack
        );

        List<PayOrder> pages = Optional.ofNullable(page.getResult()).orElse(new ArrayList<>());

        MyPageInfo<PayOrderVO> myPageInfo = new MyPageInfo<PayOrderVO>(page);
        List<PayOrderVO> pageVOS = new ArrayList<PayOrderVO>(pages.size());
        myPageInfo.setList(pageVOS);

        for(PayOrder payOrder : pages){
            PayOrderVO vo = new PayOrderVO();
            vo.setPayOrder(payOrder);
            pageVOS.add(vo);
        }
        return R.ok(myPageInfo);
    }

    @ApiOperation(tags = SEAGGER_TAGS, value = "现金收银", httpMethod = "POST")
    @PostMapping("/caseSouYin")
    public R caseSouYin(@RequestBody CaseSouYinAO caseSouYinAO){
        caseSouYinAO.check();
        String payDtoId = payService.caseSouYin(caseSouYinAO.getMoney());
        PayUtils.payFinish(payDtoId, PayOrder.PayType.payType_cash);
        return R.ok();
    }

    @ApiOperation(tags = SEAGGER_TAGS, value = "退款", httpMethod = "POST")
    @PostMapping("/payBack")
    public R payBack(@RequestBody PayBackAO payBackAO){
        payBackAO.check();

        String payBackDtoId = PayUtils.createPayBackDtoId();
        PayUtils.payBack(payBackAO.getPayDtoId(), payBackDtoId, payBackAO.getMoney());

        return R.ok();
    }

    @ApiOperation(tags = SEAGGER_TAGS, value = "退款记录", httpMethod = "POST")
    @PostMapping("/selectPayBackOrder")
    public R<MyPageInfo<PayBackVO>> selectPayBackOrder(@RequestBody SelectPayBackOrderAO selectPayBackOrderAO){
        String payBackDtoId = Optional.ofNullable(selectPayBackOrderAO).map(o -> o.getPayBackDtoId()).map(o -> o.trim()).filter(o -> !o.equals("")).orElse(null);
        String payDtoId = Optional.ofNullable(selectPayBackOrderAO).map(o -> o.getPayDtoId()).map(o -> o.trim()).filter(o -> !o.equals("")).orElse(null);
        BigDecimal backMoneyBegin = Optional.ofNullable(selectPayBackOrderAO).map(o -> o.getBackMoneyBegin()).orElse(null);
        BigDecimal backMoneyEnd = Optional.ofNullable(selectPayBackOrderAO).map(o -> o.getBackMoneyEnd()).orElse(null);
        Timestamp backTimeBegin = Optional.ofNullable(selectPayBackOrderAO).map(o -> o.getBackTimeBegin()).orElse(null);
        Timestamp backTimeEnd = Optional.ofNullable(selectPayBackOrderAO).map(o -> o.getBackTimeEnd()).orElse(null);

        // 数据查询
        Page page = PageHelper.startPage(selectPayBackOrderAO.getPageNum(), selectPayBackOrderAO.getPageSize());

        payBackMapper.selectMyList(
                payBackDtoId, payDtoId, backMoneyBegin, backMoneyEnd, backTimeBegin, backTimeEnd
        );

        List<PayBack> pages = Optional.ofNullable(page.getResult()).orElse(new ArrayList<>());

        MyPageInfo<PayBackVO> myPageInfo = new MyPageInfo<PayBackVO>(page);
        List<PayBackVO> pageVOS = new ArrayList<PayBackVO>(pages.size());
        myPageInfo.setList(pageVOS);

        for(PayBack payBack : pages){
            PayBackVO vo = new PayBackVO();
            vo.setPayBack(payBack);
            pageVOS.add(vo);
        }
        return R.ok(myPageInfo);
    }

}
